// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import AutomaticRecode from "../../Commons/AutomaticRecode";
import UIManager from "../../Managers/UIManager";
import EquipManager from "../Equips/EquipManager";
import PetManager from "./PetManager";
import PetTemplate from "./PetTemplate";


export default class PetEntity extends AutomaticRecode {

    id: number;
    level: number;
    exp: number;
    isFight: boolean;
    evolveLevel: number;

    constructor(value: any) {
        super();
        this.init(value);
        this.evolveLevel = value.evolveLevel == null ? 0 : value.evolveLevel;
    }

    get petTp(): PetTemplate {
        return PetManager.single.getPetTemplate(this.id);
    }

    get attributeDes0(): string {
        var tp = EquipManager.single.getAttributeTemplate(this.petTp.attributeId0);
        var str = `${tp.attributeName} +${this.attributeValue0}%`;
        return str;
    }
    get attributeDes1(): string {
        var tp = EquipManager.single.getAttributeTemplate(this.petTp.attributeId1);
        var str = `${tp.attributeName} +${this.attributeValue1}%`;
        return str;
    }


    get attributeValue0(): number {
        return (this.level - 1) * this.petTp.attributeGrow + this.petTp.attributeValue0;
    }
    get attributeValue1(): number {
        return (this.level - 1) * this.petTp.attributeGrow + this.petTp.attributeValue1;
    }
    get expValue(): number {
        return this.petTp.exp;
    }

    getExp(_exp: number) {
        this.exp += _exp;
        if (this.exp < this.upgradeExp) {
            UIManager.single.showTips(`宠物<${this.petTp.petName}>经验 +${_exp}。`);
        }
        while (this.exp >= this.upgradeExp) {
            if (this.level >= this.maxLevel) {
                UIManager.single.showTips(`宠物<${this.petTp.petName}>已达等级上限，进化宠物可提高等级上限。`);
                break;
            }
            this.exp -= this.upgradeExp;
            this.level++;
            UIManager.single.showTips(`宠物<${this.petTp.petName}>升级。`);
        }
    }

    get upgradeExp(): number {
        return this.level * this.level + this.level * 2;
    }

    get maxLevel(): number {
        return this.evolveLevel * this.petTp.maxLevel + this.petTp.maxLevel;
    }

    get evolveCost(): number {
        return this.evolveLevel * this.petTp.evolveCost + this.petTp.evolveCost;
    }
}

